package cn.kgc.kgde.bookstore.controller;

import cn.kgc.kgde.bookstore.entity.Book;
import cn.kgc.kgde.bookstore.entity.ResultInfo;
import cn.kgc.kgde.bookstore.mapper.BookMapper;
import cn.kgc.kgde.bookstore.service.BookService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author myt
 * @since 2022-05-23
 */
@CrossOrigin
@Configuration
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    /**
     * 商品列表
     * 前台
     */
    @PostMapping("/listBook")
    public ResultInfo listBook(@RequestParam(defaultValue = "1")Integer pageNum, Book book){
        IPage<Book> iPage=this.bookService.findBook(book,pageNum,24);
        return new ResultInfo(200,"展示列表",iPage);
    }

    /**
     * 后台
     *
     */
    @PostMapping("/listBook1")
    public ResultInfo listBook1(@RequestParam(defaultValue = "1")Integer pageNum, Book book){
        IPage<Book> iPage=this.bookService.findBook1(book,pageNum,12);
        return new ResultInfo(200,"展示列表",iPage);
    }
    //根据id查询
    @GetMapping("/findById")
    public ResultInfo findById(Long id){
        Book book = this.bookService.findById( id );
        return new ResultInfo(200,"book查看",book);
    }
//    /**
//     * 作者遍历
//     */
//    @GetMapping("/listAuthor")
//    public ResultInfo listAuthor(){
//        QueryWrapper<Book> wrapper = new QueryWrapper<>();
//        wrapper.select("author");
//        List<Book> bookList = this.bookMapper.selectList(wrapper);
//        return new ResultInfo(200,"作者查询",bookList);
//    }
//    /**
//     * 出版社遍历
//     */
//    @GetMapping("/listPress")
//    public ResultInfo listPress(){
//        QueryWrapper<Book> wrapper =new QueryWrapper<>();
//        wrapper.select("press");
//        List<Book> bookList = this.bookMapper.selectList(wrapper);
//        return new ResultInfo(200,"出版社查询",bookList);
//    }
    /**
     * 选择状态（上架，未上架）
     */
    @PostMapping("/state")
    public ResultInfo state(Integer state){
        System.out.println("state = " + state);
        List<Book> bookList =this.bookService.listState(state);
        return new ResultInfo(200,"上架状态显示",bookList);
    }
    /**
     * 添加商品
     */
    @PostMapping("/addBook")
    public ResultInfo addBook( Book book){
        this.bookService.addBook(book);
        return new ResultInfo(200,"添加成功");
    }
    /**
     * 修改商品
     */
    @PostMapping("/modifyBook")
    public ResultInfo modifyBook( Book book){
        System.out.println("book = " + book);
        this.bookService.modify(book);
        return new ResultInfo(200,"修改商品已运行");
    }
    /**
     * 删除商品
     */
    @PostMapping("/deleteBook")
    public ResultInfo deleteBook(Book book){
        this.bookService.delete(book);
        return new ResultInfo(200,"已删除商品");
    }

    /**
     * 后台 --  修改商品状态 - 上架/下架
     * modifyState
    * */
    @PostMapping("/modifyState/{id}/{state}")
    public ResultInfo modifyState(@PathVariable Long id,@PathVariable Integer state){
        this.bookService.modifyState( id,state );
        return new ResultInfo(200,"商品状态已更新");
    }

    @GetMapping("/bookDetail")
    public ResultInfo bookDetail(Long bookId){
        Book book = this.bookService.getById(bookId);
        return new ResultInfo(200,"书籍详情",book);
    }
}
